﻿# -*- coding: utf-8 -*-
"""
实现一个 copy 函数，可以从 a 目录 copy 文件到 b 目录
"""
from pathlib import Path
from shutil import copy2


# 执行复制
def Copy(src_path, des_path):
    all_path = src_path.iterdir()
    for i in all_path:
        if i.is_file():
            # print(i,'is file')
            des = des_path / i.name
            # print(des)
            # des.write_bytes(i.read_bytes())
            copy2(i, des)
        else:
            des = des_path / i.name
            des.mkdir(exist_ok=True)
            Copy(i, des)


# 检查路径并格式化
def s2d(src_path, des_path):
    if not isinstance(src_path, Path):
        src_path = Path(src_path)
    if not isinstance(des_path, Path):
        des_path = Path(des_path)
    if not src_path.is_dir():
        raise TypeError('路径有误！')
    if not des_path.is_dir():
        des_path.mkdir()
    print('-----------copy start--------')
    Copy(src_path, des_path)
    print('-----------copy end----------')


if __name__ == '__main__':
    s2d('c:/Temp/a', 'c:/a')
# 可以不用考虑路径类型试下
